package org.example.controller.user;

import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.entity.user.UTrolley;
import org.example.service.user.TrolleyService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class TrolleyController {
    /**
     * 服务对象
     */
    @Resource
    private TrolleyService trolleyService;

    //TODO 新增
    /**
     * 添加购物车(用户)
     */
    @HasRole(role = "用户")
    @RequestMapping(value = "/addTrolley", method = POST)
    public Result addTrolley(@RequestBody UTrolley trolley) {
        Long userId = trolley.getUserId();
        Long storeId = trolley.getStoreId();
        int num = trolley.getNum();



        UTrolley uTrolley = trolleyService.addTrolley(userId,storeId,num);
        return new Result(ResultCode.SUCCESS, uTrolley);
    }

    //TODO 查询
    /**
     * 获取购物车列表(用户)
     */
    @HasRole(role = "用户")
    @RequestMapping(value = "/getTrolley", method = POST)
    public Result getTtrolley() {
        List<UTrolley> list = trolleyService.getTtrolley();
        return new Result(ResultCode.SUCCESS, list);
    }

    //TODO 更新

    /**
     * 修改购物车数量(用户)
     */
    @HasRole(role = "用户")
    @RequestMapping(value = "/updateTrolleyNum", method = POST)
    public Result updateTrolleyNum(@RequestBody UTrolley trolley) {
        Long id = trolley.getId();
        Integer num = trolley.getNum();
        trolleyService.updateTrolleyNum(id, num);
        return new Result(ResultCode.SUCCESS, null);
    }

    //TODO 删除
    /**
     * 删除购物车(用户)
     */
    @HasRole(role = "用户")
    @RequestMapping(value = "deleteTrolley", method = POST)
    public Result deleteTrolley(@RequestBody UTrolley trolley) {
        Long id = trolley.getId();
        String status = trolleyService.deleteTrolley(id);
        return new Result(ResultCode.SUCCESS, status);
    }

}
